﻿using System;
using System.Web;
using System.Net.Mail;
using System.Web.UI;
using SimplePMT.Data;
using SimplePMT.Control;
using System.Text;
using System.Collections.Generic;
using Castle.ActiveRecord;
using NHibernate.Expression;
using SimplePMT.Components;

namespace SimplePMT
{
    public partial class ReleaseManager : Components.BasePage
    {
        public string viewRendered = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ReleaseManager), this.Page);
            ValidateSession();
            Control.ReleaseMangementControl view = this.LoadControl("./Control/ReleaseMangementControl.ascx") as Control.ReleaseMangementControl;
            view.AdminAccess = AdminAccess;
            viewRendered = view.ToString();
        }

        [AjaxPro.AjaxMethod()]
        public bool SaveRelease(int TaskContainerID, int ReleaseID, int Major, int Minor, int Build, int Revision, string ReleaseCutoffDate, string Notes, string DateReleased)
        {

            Release rel = Release.Find(ReleaseID);
            rel.TaskContainerID = TaskContainerID;
            rel.Major = Major;
            rel.Minor = Minor;
            rel.Build = Build;
            rel.Revision = Revision;
            rel.ReleaseDateCutoff = Convert.ToDateTime(ReleaseCutoffDate);
            rel.Notes = Notes;
            rel.DateReleased = Convert.ToDateTime(DateReleased);
            rel.Save();

            return true;
        }

        [AjaxPro.AjaxMethod()]
        public string BuildReleaseList(int TaskContainerID)
        {
            Release[] releaseItems = Release.FindAll(Expression.Eq("TaskContainerID", TaskContainerID));
            string[] releaseList = new string[releaseItems.Length + 2];
            using (new SessionScope())
            {
                int i = 2;
                releaseList[0] = "0=SELECT RELEASE";
                releaseList[1] = "-1=Create New Release";
                foreach (Release release in releaseItems)
                {
                    string date = "Released on " + release.DateReleased.ToShortDateString();
                    if (release.DateReleased.ToShortDateString().Equals("1/1/1900"))
                    {
                        date = "";
                    }
                    releaseList[i] = release.ID + "=" + release.Major + "." + release.Minor + "." + release.Build + " " + date;
                    i++;
                }
            }
            string list = FormElement.Select(200, "ReleaseID", 0, releaseList, " OnChange=\"GetReleaseOption(this.options[this.options.selectedIndex].value);\"", true);
            return list;
        }
        [AjaxPro.AjaxMethod()]
        public bool CreateRelease(int TaskContainerID, int Major, int Minor, int Build, string ReleaseCutoffDate, string Notes)
        {

            Release rel = new Release();
            rel.TaskContainerID = TaskContainerID;
            rel.Major = Major;
            rel.Minor = Minor;
            rel.Build = Build;
            rel.Revision = 0;
            rel.ReleaseDateCutoff = Convert.ToDateTime(ReleaseCutoffDate);
            rel.DateReleased = Convert.ToDateTime("1/1/1900");
            rel.Notes = Notes;
            rel.Save();

            return true;
        }

        [AjaxPro.AjaxMethod()]
        public object[] GetBuildObject(int id)
        {
            Release r = Release.Find(id);
            return new object[] { r.Major, r.Minor, r.Build, r.Revision, r.ReleaseDateCutoff.ToShortDateString(), r.Notes, r.DateReleased.ToShortDateString() };
        }
    }

}
